//
//  ProfileViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/16.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

struct YSGOrderNumbers: HandyJSON {
    struct Item: HandyJSON {
        var number: Int = 0
        var orderStatus: Int = 0
    }
    
    var orderNumbsers: [Item] = []
    var orderType: Int = 0
}

struct ProfileViewModel {
    
    let datas: [YSGOrderNumbers]
    
    static func changePassword(old: String, new: String, finished: @escaping (_ msg: String?, _ code: HttpCode) -> Void) {
        let paras = ["password": old, "newPassword": new]
        HttpTool.shared.request(target: ApiManager.changePassword(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    static func logout(finished: @escaping (_ code: HttpCode, _ msg: String?) -> Void) {
        HttpTool.shared.request(target: ApiManager.logout, success: { (json, code) in
            finished(code, json["message"].description)
        }) { errorMsg in
            finished(.failure, errorMsg)
        }
    }
    
    static func getOrderNumbers(userId: Int, finished: @escaping ([YSGOrderNumbers], String?) -> Void) {
        let paras = ["userId": userId]
        HttpTool.shared.request(target: ApiManager.getOrderNumbers(paras: paras), success: { (json, code) in
            YSGLog("获取各订单数量: \(json)")
            switch code {
            case .success:
                if let datas = JSONDeserializer<YSGOrderNumbers>.deserializeModelArrayFrom(json: json["data"].description) as? [YSGOrderNumbers] {
                    finished(datas, nil)
                }
            case .failure:
                finished([], json["message"].description)
            }
        }) { error in
            finished([], error)
        }
    }
    
    /// 修改姓名
    static func changeNickname(nickname: String, finished: @escaping (String?, HttpCode) -> Void) {
        let params = ["nickName": nickname]
        HttpTool.shared.request(target: ApiManager.changeNickname(paras: params), success: { (json, code) in
            finished(json["message"].description, code)
        }) { error in
            finished(error, .failure)
        }
    }
}

